import koaRouter from 'koa-router'
import connection from '../config/mysqlConfig'
import jwt from 'jsonwebtoken'
const router = koaRouter()

// 获取轮播图
router.get('/music', async (ctx) => {
    try {
        // 判断用户是否有权限获取所有轮播图
        const user = await new Promise((resolve, reject) =>{
            connection.query('select * from users where name=?', ctx.username, (err, results) => {
                if(err) reject(err)
                resolve(results)
            })
        })
        if(user) {
            const result = await new Promise((resolve, reject) => {
                connection.query('select * from songs', (err, results) => {
                    if (err) reject(err)
                    resolve(results)
                })
            })
            // result = result.map(item =>{
            //     return {
            //         url: `http://localhost:${port}/${item.filename}`
            //     }
            // })
            ctx.body = {
                code: 1,
                list: result
            }
        } else {
            ctx.body = {
                code: 0,
                msg: '该用户不存在'
            }
        }


    } catch (e) {
        ctx.body = {
            code: 0,
            error: e.message
        }
    }
})
// 删除轮播图
router.post('/delete/slide', async (ctx) => {
    try {
        const {
            id
        } = ctx.request.body
        //    只有管理员才能删，用户的role字段为1，则为管理员
        let token = ctx.request.header.authorization
        // 对token进行解密
        let decoded = jwt.verify(token, 'mengliang')
        if (decoded && decoded.message) {
            ctx.body = {
                code: 0,
                msg: decoded.message
            }
        } else {
            const user = await new Promise((resolve, reject) => {
                connection.query('select * from users where name=?', decoded.name, (err, results) => {
                    if (err) reject(err)
                    resolve(results)
                })
            })
            if (user.length > 0) {
                // 找到用户了
                if (user[0].role == 1) {
                    // 是管理员
                    await new Promise((resolve, reject) => {
                        connection.query('delete from slides where id=?', id, (err) => {
                            if (err) reject(err)
                            resolve()
                        })
                    })
                    ctx.body = {
                        code: 1,
                        msg: '删除成功'
                    }
                } else {
                    // 不是管理员
                    ctx.body = {
                        code: 0,
                        msg: '此用户无权限删除轮播图'
                    }
                }
            } else {
                ctx.body = {
                    code: 0,
                    msg: '用户不合法'
                }
            }
        }
    } catch (e) {
        ctx.body = {
            code: 0,
            error: e.message
        }
    }
})

// 保存音乐相关
router.post('/save/music', async(ctx) => {
    try{
        const {
            name,
            filename,
            url,
            singer
        } = ctx.request.body
        console.log(1111, name)
        await new Promise((resolve, reject) => {
            connection.query('insert into songs set ?', {
                name,
                filename,
                url,
                singer
            }, (err) => {
                if(err) reject(err)
                resolve()
            })
        })
        ctx.body = {
            code: 1,
            msg: '上传成功'
        }
        
    } catch(e ){
        ctx.body = {
            code: 0,
            error: e.message
        }
    }
})

export default router